package com.ybkj.daijia.server.controller;

import com.ybkj.daijia.common.Result;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.page.Pageable;
import com.ybkj.daijia.page.domain.Sort;
import com.ybkj.daijia.server.gasstation.GasPrice;
import com.ybkj.daijia.server.gasstation.GasStation;
import com.ybkj.daijia.server.gasstation.PaymentsRecord;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.GasDiscountService;
import com.ybkj.daijia.server.service.GasOrderService;
import com.ybkj.daijia.server.service.GasStationReviewService;
import com.ybkj.daijia.server.service.GasStationService;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "gasstation")
public class GasStationController {


    @Autowired
    private GasStationService gasStationService;

    @Autowired
    private GasStationReviewService gasStationReviewService;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private ApplicationEventMulticaster applicationEventMulticaster;

    @Autowired
    private GasOrderService gasOrderService;
    @Autowired
    private GasDiscountService gasDiscountService;

    /**
     * 跳转加油站列表.
     */
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ModelAndView list() {
        return new ModelAndView();
    }

    /**
     * 加油站订单列表
     */
    @RequestMapping(value = "orderList", method = RequestMethod.GET)
    public ModelAndView orderList() {
        return new ModelAndView();
    }

    /**
     * 订单列表数据
     */
    @RequestMapping(value = "orderListData", method = RequestMethod.GET)
    public @ResponseBody
    Page<Map<String, Object>> orderListData(
        @RequestParam(required = false, value = "statusSelect") String statusSelect,
        @RequestParam(required = false, value = "params") String params,
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "endTime") String endTime,
        @RequestParam(required = false, value = "page") Integer page,
        @RequestParam(required = false, value = "rows") Integer rows)
        throws ParseException {
        Page<Map<String, Object>> mapPage = new Page<Map<String, Object>>() {
            @Override
            public int getTotalPages() {
                return 0;
            }

            @Override
            public long getTotalElements() {
                return 0;
            }

            @Override
            public int getNumber() {
                return 0;
            }

            @Override
            public int getSize() {
                return 0;
            }

            @Override
            public int getNumberOfElements() {
                return 0;
            }

            @Override
            public List<Map<String, Object>> getContent() {
                return null;
            }

            @Override
            public boolean hasContent() {
                return false;
            }

            @Override
            public Sort getSort() {
                return null;
            }

            @Override
            public boolean isFirst() {
                return false;
            }

            @Override
            public boolean isLast() {
                return false;
            }

            @Override
            public boolean hasNext() {
                return false;
            }

            @Override
            public boolean hasPrevious() {
                return false;
            }

            @Override
            public Pageable nextPageable() {
                return null;
            }

            @Override
            public Pageable previousPageable() {
                return null;
            }

            @Override
            public Iterator<Map<String, Object>> iterator() {
                return null;
            }
        };
        return mapPage;
    }

    /**
     * 导出订单
     *
     * @throws IOException
     */
    @RequestMapping(value = "exportOrder", method = RequestMethod.GET)
    public void exportOrder(
        @RequestParam(required = false, value = "statusSelect") String statusSelect,
        @RequestParam(required = false, value = "params") String params,
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "endTime") String endTime,
        HttpServletRequest req, HttpServletResponse response) throws IOException {

    }

    /**
     * 跳转添加加油站界面.
     */
    @RequestMapping(value = "add", method = RequestMethod.GET)
    public ModelAndView add() {
//		Subject subject = SecurityUtils.getSubject();
//		if (subject.isPermitted("gasStation")) {
//			ModelAndView mv = new ModelAndView();
//			String serivceNo = GasServiceNoUtil.getSerivceNo();
//			Page<GasStation> page = gasStationService.queryStationByParams(null, null, null, null, null, null, null);
//			if(page.getTotalElements()>0){
//				for (GasStation gasStation : page.getContent()) {
//					if(gasStation.getServiceNo().equals(serivceNo)){
//						serivceNo = GasServiceNoUtil.getSerivceNo();
//					}
//					if(gasStation.getServiceNo().equals(serivceNo)){
//						break;
//					}
//
//				}
//			}
//			mv.addObject("serviceNo", serivceNo);
//			mv.addObject("companys", companyService.findAll());
//
//			mv.setViewName("gasstation/add");
//
//			return mv;
//		} else {
//			throw new NoPermissionException();
//		}
        return new ModelAndView();
    }

    /**
     * 保存加油站.
     */
    @RequestMapping(value = "create", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> create(GasStation gasstation) {
//		Subject subject = SecurityUtils.getSubject();
//		if (subject.isPermitted("gasStation")) {
//			Map<String, Object> map = new HashMap<String, Object>();
//			gasStationService.create(gasstation);
//			map.put("success", true);
//			map.put("message", "添加成功");
//			return map;
//		} else {
//			throw new NoPermissionException();
//		}
        Map<String, Object> map = new HashMap<String, Object>();
        return map;
    }

    /**
     * 删除加油站.
     */
    @RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> delete(@PathVariable("id") Long id) {
//	  Subject subject = SecurityUtils.getSubject();
//
//	  if (subject.isPermitted("gasstationDelete")) {
//	    Map<String, Object> map = new HashMap<String, Object>();
//
//	    String delete = gasStationService.delete(id);
//	    if (StringUtils.isBlank(delete)) {
//	      map.put("success", true);
//	      map.put("message", "删除成功!");
//	    } else {
//	      map.put("success", false);
//	      map.put("message", delete);
//	    }
//	    return map;
//	  } else {
//	    throw new NoPermissionException();
//	  }
        Map<String, Object> map = new HashMap<String, Object>();
        return map;
    }

    /**
     * 查询加油站数据.
     */
    @RequestMapping(value = "listData", method = RequestMethod.GET)
    public @ResponseBody
    Page<GasStation> listData(
        @RequestParam(required = false, value = "name") String name,
        @RequestParam(required = false, value = "serviceNo") String serviceNo,
        @RequestParam(required = false, value = "address") String address,
        @RequestParam(required = false, value = "contactsPhone") String contactsPhone,
        @RequestParam(required = false, value = "page") Integer page,
        @RequestParam(required = false, value = "rows") Integer rows) {
//		Subject subject = SecurityUtils.getSubject();
//
//		if (subject.isPermitted("gasstation")) {
//			page = Pager.getPage();
//			rows = Pager.getLimit();
//			Principal principal = (Principal)subject.getPrincipal();
//			if (principal.isCompanyType()) {
//			  return gasStationService.queryStationByParams(name, serviceNo,
//			      null, address, contactsPhone, page, rows);
//			} else {
//			  return gasStationService.queryStationByParams(name, serviceNo,
//			      principal.getCompanyId(), address, contactsPhone, page, rows);
//			}
//		} else {
//			throw new NoPermissionException();
//		}
        Page<GasStation> gasStationPage = new Page<GasStation>() {
            @Override
            public int getTotalPages() {
                return 0;
            }

            @Override
            public long getTotalElements() {
                return 0;
            }

            @Override
            public int getNumber() {
                return 0;
            }

            @Override
            public int getSize() {
                return 0;
            }

            @Override
            public int getNumberOfElements() {
                return 0;
            }

            @Override
            public List<GasStation> getContent() {
                return null;
            }

            @Override
            public boolean hasContent() {
                return false;
            }

            @Override
            public Sort getSort() {
                return null;
            }

            @Override
            public boolean isFirst() {
                return false;
            }

            @Override
            public boolean isLast() {
                return false;
            }

            @Override
            public boolean hasNext() {
                return false;
            }

            @Override
            public boolean hasPrevious() {
                return false;
            }

            @Override
            public Pageable nextPageable() {
                return null;
            }

            @Override
            public Pageable previousPageable() {
                return null;
            }

            @Override
            public Iterator<GasStation> iterator() {
                return null;
            }
        };
        return gasStationPage;
    }

    /**
     * 导出.
     */
    @RequestMapping(value = "exportGasStation", method = RequestMethod.GET)
    public void exportOrder(
        @RequestParam(required = false, value = "serviceNo") String serviceNo,
        @RequestParam(required = false, value = "address") String address,
        @RequestParam(required = false, value = "contactsPhone") String contactsPhone,
        HttpServletRequest req, HttpServletResponse response)
        throws ParseException {
    }

    /**
     * 跳转编辑加油站界面.
     */
    @RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("id") Long gsId) {
//		Subject subject = SecurityUtils.getSubject();
//		if (subject.isPermitted("gasStation")) {
//			ModelAndView mv = new ModelAndView();
//			GasStation gasStation = gasStationService.queryById(gsId);
//			mv.addObject("gsid", gsId);
//			mv.addObject("companys", companyService.findAll());
//			mv.addObject("gasStation", gasStation);
//			mv.setViewName("gasstation/edit");
//
//			return mv;
//		} else {
//			throw new NoPermissionException();
//		}
        return new ModelAndView();
    }

    /**
     * 修改加油站.
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> update(GasStation gasStation) {
//		Subject subject = SecurityUtils.getSubject();
//
//		if (subject.isPermitted("gasstation")) {
//			Map<String, Object> map = new HashMap<String, Object>();
//			map = gasStationService.update(gasStation);
//			return map;
//		} else {
//			throw new NoPermissionException();
//		}
        Map<String, Object> map = new HashMap<String, Object>();
        return map;
    }

    /**
     * 修改加油站登录密码.
     */
    @RequestMapping(value = "changepassword", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> changePassword(Long id,
        String password) {
//		Subject subject = SecurityUtils.getSubject();
//
//		if (subject.isPermitted("gasstation")) {
//			Map<String, Object> map = new HashMap<String, Object>();
//			map = gasStationService.changePassword(id, password);
//			return map;
//		} else {
//			throw new NoPermissionException();
//		}
        Map<String, Object> map = new HashMap<String, Object>();
        return map;
    }

    /**
     * 修改加油站登录账号冻结.
     */
    @RequestMapping(value = "changeFreeze", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> changeFreeze(Long id,
        Boolean freeze) {
//		Subject subject = SecurityUtils.getSubject();
//
//		if (subject.isPermitted("gasstation")) {
//			Map<String, Object> map = new HashMap<String, Object>();
//			map = gasStationService.changeFreeze(id, freeze);
//			return map;
//		} else {
//			throw new NoPermissionException();
//		}
        Map<String, Object> map = new HashMap<String, Object>();
        return map;
    }

    @RequestMapping(value = "editGasPrice/{id}", method = RequestMethod.GET)
    public ModelAndView editGasPrice(@PathVariable("id") Long gsid) {
        return new ModelAndView();
    }

    @RequestMapping(value = "saveGasPrice", method = RequestMethod.POST)
    public @ResponseBody
    Result<GasPrice> saveGasPrice(
        @RequestParam(required = true, value = "id") Long id,
        @RequestParam(required = true, value = "price") BigDecimal price) {

        Result<GasPrice> result = new Result<GasPrice>();
        return result;
    }

    /**
     * 客户等级修改单个加油站折扣
     */
    @RequestMapping(value = "saveGasDiscount", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> saveGasDiscount(
        @RequestParam(required = true, value = "gsId") Long gsId,
        @RequestParam(required = true, value = "passengerGradeId") Long passengerGradeId,
        @RequestParam(required = true, value = "discount") Double discount) {

        Map<String, Object> map = new HashMap<String, Object>();
        return map;
    }

    /**
     * 客户等级修改全部加油站折扣
     */
    @RequestMapping(value = "saveGasDiscountAll", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> saveGasDiscountAll(
        @RequestParam(required = true, value = "passengerGradeId") Long passengerGradeId,
        @RequestParam(required = true, value = "discount") Double discount) {

        Map<String, Object> map = new HashMap<String, Object>();
        return map;
    }

    @RequestMapping(value = "saveGasPriceStraightDown", method = RequestMethod.POST)
    public @ResponseBody
    Result<GasPrice> saveGasPriceStraightDown(
        @RequestParam(required = true, value = "id") Long id,
        @RequestParam(required = false, value = "straightDown") String straightDown) {

        Result<GasPrice> result = new Result<GasPrice>();
        return result;
    }

    @RequestMapping(value = "saveGasPriceSort", method = RequestMethod.POST)
    public @ResponseBody
    Result<GasPrice> saveGasPriceSort(
        @RequestParam(required = true, value = "id") Long id,
        @RequestParam(required = true, value = "sort") Integer sort) {
//
        Result<GasPrice> result = new Result<GasPrice>();
        return result;
    }

    /*
     * 资金流水
     */
    @RequestMapping(value = "paymentsRecord/{id}", method = RequestMethod.GET)
    public ModelAndView paymentsRecord(
        @PathVariable("id") Long gsid,
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "endTime") String endTime) {

        return new ModelAndView();
    }

    /*
     * 资金流水
     */
    @RequestMapping(value = "paymentsRecordAll", method = RequestMethod.GET)
    public ModelAndView paymentsRecordAll(
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "endTime") String endTime,
        @RequestParam(required = false, value = "gasStation") String gasStation) {

        return new ModelAndView();
    }


    @RequestMapping(value = "savePaymentsRecord", method = RequestMethod.POST)
    public synchronized @ResponseBody
    Result<PaymentsRecord> savePaymentsRecord(
        Long gsid, BigDecimal money, String memo) {

        Result<PaymentsRecord> result = new Result<PaymentsRecord>();
        return result;

    }

    @RequestMapping(value = "paymentsRecordExport/{id}", method = RequestMethod.GET)
    public void paymentsExport(
        @PathVariable("id") Long gsid,
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "endTime") String endTime,
        HttpServletRequest req, HttpServletResponse response)
        throws IOException {

    }

    @RequestMapping(value = "paymentsRecordAllExport", method = RequestMethod.GET)
    public void paymentsRecordAllExport(
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "endTime") String endTime,
        @RequestParam(required = false, value = "gasStation") String gasStation,
        HttpServletRequest req, HttpServletResponse response)
        throws IOException {

    }


    /**
     * 跳转评价列表.
     */
    @RequestMapping(value = "review/{gsid}", method = RequestMethod.GET)
    public ModelAndView reviewList(@PathVariable("gsid") Long gsid,
        @RequestParam(required = false, value = "page") Integer page,
        @RequestParam(required = false, value = "rows") Integer rows) {
//		Subject subject = SecurityUtils.getSubject();
//
//		if (subject.isPermitted("gasstation")) {
//
//			ModelAndView mv = new ModelAndView();
//
//			GasStation gasStation = gasStationService.queryById(gsid);
//
//			if (gasStation != null) {
//				mv.addObject("gasStationName", gasStation.getName());
//				mv.addObject("serviceNo", gasStation.getServiceNo());
//			} else {
//				mv.addObject("gasStationName", "");
//				mv.addObject("serviceNo", "");
//			}
//			mv.addObject("gsid", gsid);
//			mv.setViewName("gasstation/review");
//			return mv;
//		} else {
//			throw new NoPermissionException();
//		}
        return new ModelAndView();
    }

    /**
     * 查询评价数据.
     */
    @RequestMapping(value = "reviewData", method = RequestMethod.GET)
    public @ResponseBody
    Page<Map<String, Object>> reviewData(
        @RequestParam("gsid") Long gsid,
        @RequestParam(required = false, value = "page") Integer page,
        @RequestParam(required = false, value = "rows") Integer rows) {
//		Subject subject = SecurityUtils.getSubject();
//
//		if (subject.isPermitted("gasstation")) {
//			page = Pager.getPage();
//			rows = Pager.getLimit();
//			return gasStationReviewService
//					.queryByGasStationId(gsid, page, rows);
//		} else {
//			throw new NoPermissionException();
//		}
        Page<Map<String, Object>> mapPage = new Page<Map<String, Object>>() {
            @Override
            public int getTotalPages() {
                return 0;
            }

            @Override
            public long getTotalElements() {
                return 0;
            }

            @Override
            public int getNumber() {
                return 0;
            }

            @Override
            public int getSize() {
                return 0;
            }

            @Override
            public int getNumberOfElements() {
                return 0;
            }

            @Override
            public List<Map<String, Object>> getContent() {
                return null;
            }

            @Override
            public boolean hasContent() {
                return false;
            }

            @Override
            public Sort getSort() {
                return null;
            }

            @Override
            public boolean isFirst() {
                return false;
            }

            @Override
            public boolean isLast() {
                return false;
            }

            @Override
            public boolean hasNext() {
                return false;
            }

            @Override
            public boolean hasPrevious() {
                return false;
            }

            @Override
            public Pageable nextPageable() {
                return null;
            }

            @Override
            public Pageable previousPageable() {
                return null;
            }

            @Override
            public Iterator<Map<String, Object>> iterator() {
                return null;
            }
        };
        return mapPage;
    }

    /**
     * 添加评价.
     */
    @RequestMapping(value = "review", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> review(
        @RequestParam("gasStationId") Long gasStationId,
        @RequestParam("gasStationName") String gasStationName,
        @RequestParam("serviceNo") String serviceNo,
        @RequestParam("clientName") String clientName,
        @RequestParam("clientPhone") String clientPhone,
        @RequestParam("score") Double score,
        @RequestParam("content") String content) {
        Map<String, Object> map = new HashMap<String, Object>();
        return map;
    }

    /**
     * 删除评价.
     */
    @RequestMapping(value = "deleteReview/{id}", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> deleteReview(
        @PathVariable("id") Long id) {
        Map<String, Object> map = new HashMap<String, Object>();
        return map;
    }

    @org.springframework.web.bind.annotation.InitBinder
    public void InitBinder(WebDataBinder dataBinder) {
    }

}
